package com.sk.cardeal.act.home.activity;

import android.os.Bundle;
import android.os.TestLooperManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.NewFixProject;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.utils.SnackUtils;
import com.sk.cardeal.view.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sk on 2017/12/25.
 */

public class NewProjectActivity extends BaseActivity implements HandleDataCallBack {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.ed_project_name)
    EditText edProjectName;
    @BindView(R.id.money_hour)
    EditText moneyHour;
    @BindView(R.id.standard_hour)
    EditText standardHour;
    @BindView(R.id.xiao_money)
    EditText xiaoMoney;
    @BindView(R.id.discount_money)
    EditText discountMoney;
    @BindView(R.id.after_discount_money)
    EditText afterMoney;
    @BindView(R.id.tv_back)
    TextView back;
    @BindView(R.id.sure)
    TextView sure;
    private LoadingDialog loadingDialog;

    private int type;

    @Override
    protected int getContentView() {
        return R.layout.activity_new_project;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        type = getIntent().getIntExtra("type", 0);
    }

    @OnClick({R.id.tv_back, R.id.sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sure:


                if (TextUtils.isEmpty(edProjectName.getText().toString())) {
                    SnackUtils.showError(mRootView, "请填写项目名称!");
                    return;
                }

                if (TextUtils.isEmpty(moneyHour.getText().toString())) {
                    SnackUtils.showError(mRootView, "请输入工时金额！");
                    return;
                }
                if (TextUtils.isEmpty(standardHour.getText().toString())) {
                    SnackUtils.showError(mRootView, "请输入标准工时!");
                    return;
                }

                if (TextUtils.isEmpty(xiaoMoney.getText().toString())) {
                    SnackUtils.showError(mRootView, "请输入小计金额！");
                    return;
                }

                if (TextUtils.isEmpty(discountMoney.getText().toString())) {
                    SnackUtils.showError(mRootView, "请输入优惠金额!");
                    return;
                }

                if (TextUtils.isEmpty(afterMoney.getText().toString())) {
                    SnackUtils.showError(mRootView, "请输入折扣后金额！");
                    return;
                }


                loadingDialog.show();
                NewFixProject newFixProject = new NewFixProject();
                newFixProject.setType(type);
                newFixProject.setAfterCutPrice(Double.parseDouble(afterMoney.getText().toString()));
                newFixProject.setCouponPrice(Double.parseDouble(discountMoney.getText().toString()));
                newFixProject.setGroupId(MyUtils.getUser(mContext).getCompanyId());
                newFixProject.setManHours(Double.parseDouble(standardHour.getText().toString()));
                newFixProject.setManHoursPrice(Double.parseDouble(moneyHour.getText().toString()));
                newFixProject.setProjectName(edProjectName.getText().toString());
                newFixProject.setTotalPrice(Double.parseDouble(xiaoMoney.getText().toString()));
                AppApi.newFixProject(newFixProject, NewProjectActivity.this, 0, NewProjectActivity.this);
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        Toast.makeText(mContext, "项目新增成功！", Toast.LENGTH_SHORT).show();
        setResult(RESULT_OK);
        finish();

    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();

    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();

    }
}
